import modelType from '@/pages/SalaryManage/constant/commonModelType';

const loginAction = /\/login/gi;

const menuModelFactory = namespace => ({
    namespace,
    state: {
        organizationResponse: null,
        organizationList: null,
        selectOrganizationCode: null,
        countryNameCode: null
    },
    reducers: {
        [modelType.SET_MULTI_STORE](state, action) {
            return {
                ...state,
                ...action.payload
            };
        },
        [modelType.CLEAR](state) {
            return {
                ...state,
                selectOrganizationCode: null,
                countryNameCode: null
            };
        }
    },

    subscriptions: {
        setup(props) {
            const { dispatch, history } = props;
            return history.listen(({ pathname }) => {
                if (String(pathname).search(loginAction) > -1) {
                    // 如果是登录登出操作清空缓存
                    dispatch({
                        type: modelType.CLEAR
                    });
                }
            });
        }
    }
});

export default menuModelFactory;
